import axios from 'axios';

// 创建axios实例
const api = axios.create({
  baseURL: 'http://localhost:8000',  // 后端API地址
  timeout: 10000,  // 请求超时时间
  withCredentials: true  // 跨域请求时携带cookies
});

// 请求拦截器
api.interceptors.request.use(
  config => {
    // 可以在这里添加统一的请求头等处理
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截器
api.interceptors.response.use(
  response => {
    // 直接返回响应数据部分，避免后续使用时需要再次处理
    return response.data;
  },
  error => {
    // 统一处理错误
    console.error('API请求错误:', error);
    return Promise.reject(error);
  }
);

// 定义类型，避免ts报错
export interface HomeStats {
  today_total: number;
  realtime_increment: number;
  platform_count: number;
  sentiment_distribution: {
    positive: number;
    neutral: number;
    negative: number;
  };
  sentiment_trend: Array<{
    hour: string;
    positive: number;
    neutral: number;
    negative: number;
  }>;
  hot_keywords: Array<{
    word: string;
    weight: number;
  }>;
  hot_sentiments: Array<{
    id: number;
    title: string;
    platform: string;
    heat_value: number;
    sentiment_type: number;
  }>;
  platform_comparison: Array<{
    platform: string;
    count: number;
  }>;
  sentiment_time_distribution: Array<{
    id: number;
    time: string;
    value: number;
  }>;
  alert_count: number;
  alert_increment: number;
  priority_targets: Array<{
    id: number;
    name: string;
    type: string;
    image: string;
  }>;
  extreme_sentiment: {
    most_positive: {
      id: number;
      title: string;
      value: number;
    } | null;
    most_negative: {
      id: number;
      title: string;
      value: number;
    } | null;
  };
  system_status: {
    crawler: {
      status: boolean;
      response_time: number;
    };
    nlp: {
      status: boolean;
      response_time: number;
    };
    api: {
      status: boolean;
      response_time: number;
    };
  };
}

// 舆情数据接口
export interface SentimentData {
  id: number;
  title: string;
  content: string;
  platform: string;
  url: string;
  author: string;
  publish_time: string;
  crawl_time: string;
  sentiment_type: number;
  sentiment_value: number;
  keywords: string[];
  heat_value: number;
  is_alert: boolean;
}

// 舆情列表响应接口
export interface SentimentListResponse {
  count: number;
  next: string | null;
  previous: string | null;
  results: SentimentData[];
}

// API接口定义
export default {
  // 首页数据
  getHomeStats(): Promise<HomeStats> {
    return api.get('/api/home-stats/');
  },
  
  // 舆情数据相关
  getSentiments(params?: any): Promise<SentimentListResponse> {
    return api.get('/api/sentiments/', { params });
  },
  getLatestSentiments(): Promise<SentimentData[]> {
    return api.get('/api/sentiments/latest/');
  },
  getHotSentiments(): Promise<SentimentData[]> {
    return api.get('/api/sentiments/hot/');
  },
  getSentimentDetail(id: number): Promise<SentimentData> {
    return api.get(`/api/sentiments/${id}/`);
  },
  deleteSentiment(id: number): Promise<any> {
    return api.delete(`/api/sentiments/${id}/`);
  },
  
  // 平台相关
  getPlatforms() {
    return api.get('/api/platforms/');
  },
  
  // 关键词相关
  getKeywords() {
    return api.get('/api/keywords/');
  },
  
  // 监控目标相关
  getMonitorTargets() {
    return api.get('/api/targets/');
  },
  getPriorityTargets() {
    return api.get('/api/targets/priority/');
  },
  
  // 预警相关
  getAlertRules() {
    return api.get('/api/alert-rules/');
  },
  getAlertLogs() {
    return api.get('/api/alert-logs/');
  },
  getUnhandledAlerts() {
    return api.get('/api/alert-logs/unhandled/');
  },
  
  // 系统状态
  getSystemStatus() {
    return api.get('/api/system-status/');
  },
  
  // 每日统计
  getDailyStatistics() {
    return api.get('/api/daily-statistics/');
  }
}; 